console.log('深拷贝')

let obj1 = {
    city: 'shanghai',
    num: 1,
    arr: ['1', 2, 3]


}
// let obj2 = obj1
const obj2 = deepClone(obj1)
obj2.city = 'beijing'
console.log(obj1.city)
/**
 * 深拷贝  
 * @param {Object} obj 要拷贝的对象
 */
function deepClone (obj) {
    if (obj == null || typeof obj !== 'object') {
        //obj不是对象数组或为null，直接返回
        return obj
    }
    //初始化返回结果
    let result
    if (obj instanceof Array) { //判断是不是数组
        result = []
    }
    else {
        result = {}
    }
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            //递归
            result[key] = deepClone(obj[key])
        }
    }
    //返回结果
    return result

}
